cmake_minimum_required(VERSION 3.10)
project(IonosphereVTEC VERSION 1.0)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置libtorch路径
set(TORCH_PATH "/mnt/c/Users/elev820/Documents/libtorch-cxx11-abi-shared-with-deps-2.5.1+cpu/libtorch")
list(APPEND CMAKE_PREFIX_PATH ${TORCH_PATH})

# 查找Torch包
find_package(Torch REQUIRED)
message(STATUS "Torch libraries: ${TORCH_LIBRARIES}")

# 设置OpenMP支持（可选，用于加速计算）
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

# 定义源文件
set(SOURCES
    src/main.cpp
    src/RinexParser.cpp
    src/Preprocessor.cpp
    src/STECCalculator.cpp
    src/VTECConverter.cpp
    src/SphericalHarmonic.cpp
    src/DCBEstimator.cpp
    src/IONEXWriter.cpp
    src/Utils.cpp
    src/GPSSatelliteOrbit.cpp
    src/SP3Interpolator.cpp
    src/Config.cpp
)

# 定义头文件
set(HEADERS
    include/Common.h
    include/RinexParser.h
    include/Preprocessor.h
    include/STECCalculator.h
    include/VTECConverter.h
    include/SphericalHarmonic.h
    include/DCBEstimator.h
    include/IONEXWriter.h
    include/Utils.h
    include/GPSSatelliteOrbit.h
    include/SP3Interpolator.h
    include/Config.h
)

# 包含头文件目录
include_directories(include)

# 添加可执行文件
add_executable(ionosphere_vtec ${SOURCES})

# 设置输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# 链接库
target_link_libraries(ionosphere_vtec ${TORCH_LIBRARIES})
if(OpenMP_CXX_FOUND)
    target_link_libraries(ionosphere_vtec OpenMP::OpenMP_CXX)
endif()

# 设置编译选项
if(MSVC)
    target_compile_options(ionosphere_vtec PRIVATE /W4)
    # 复制DLL文件到输出目录
    file(GLOB TORCH_DLLS "${TORCH_PATH}/lib/*.dll")
    add_custom_command(TARGET ionosphere_vtec
                      POST_BUILD
                      COMMAND ${CMAKE_COMMAND} -E copy_if_different
                      ${TORCH_DLLS}
                      $<TARGET_FILE_DIR:ionosphere_vtec>)
else()
    target_compile_options(ionosphere_vtec PRIVATE -Wall -Wextra)
endif()

# 设置调试信息
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")

# 启用测试（可选）
option(BUILD_TESTS "Build test programs" OFF)
if(BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()

# 添加示例数据目录（可选）
option(INSTALL_DATA "Install sample data" OFF)
if(INSTALL_DATA)
    file(COPY ${CMAKE_SOURCE_DIR}/data DESTINATION ${CMAKE_BINARY_DIR})
endif()

# 添加安装规则
install(TARGETS ionosphere_vtec DESTINATION bin)

# 打印配置信息
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Torch Path: ${TORCH_PATH}")
message(STATUS "OpenMP Support: ${OpenMP_CXX_FOUND}")

# Consolidated diagnostic and debugging options
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    # Set basic debugging flags
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -O0")
    
    # Add compiler diagnostics options
    target_compile_options(ionosphere_vtec PRIVATE 
        -Wno-error 
        -fdiagnostics-color=always
        -ftemplate-backtrace-limit=0
        -H)  # Shows include tree
endif() 